package com.tangzx.builder.encoder;

import com.tangzx.builder.SwfMovie;
import com.tangzx.builder.abc.AbcFile;
import com.tangzx.builder.context.IAbcContext;
import flash.swf.Movie;
import flash.swf.MovieEncoder;
import flash.swf.TagHandler;

public class MovieABCEncoder extends MovieEncoder {

	private IAbcContext abcContext;

	public MovieABCEncoder(TagHandler handler, IAbcContext context) {
		super(handler);
		this.abcContext = context;
	}
	
	public void mix(SwfMovie sm ) {
		AbcEncoder[] abcEncoders = new AbcEncoder[sm.abcs.size()];
		for (int i = 0; i < sm.abcs.size(); i++) {
			AbcFile abcFile = sm.abcs.get(i);
			AbcEncoder abcEncoder = new AbcEncoder(abcFile, abcContext);
			abcEncoders[i] = abcEncoder;
		}

		for (AbcEncoder encoder : abcEncoders) {
			//混淆静态方法和常量
			encoder.mixClassInfo();
		}
		for (AbcEncoder encoder : abcEncoders) {
			// 混淆字符常量名
			encoder.mixClassName();
		}
		for (AbcEncoder encoder : abcEncoders) {
			// 混淆方法名
			encoder.mixMethod();
		}
		for (AbcEncoder encoder : abcEncoders) {
			// 混淆字段名
			encoder.mixField();
		}
		for (AbcEncoder encoder : abcEncoders) {
			// 混淆包名
			encoder.mixNS();
		}
	}

	@Override
	public void export(Movie m) {
		SwfMovie sm = (SwfMovie) m;

		AbcEncoder[] abcEncoders = new AbcEncoder[sm.abcs.size()];
		for (int i = 0; i < sm.abcs.size(); i++) {
			AbcFile abcFile = sm.abcs.get(i);
			AbcEncoder abcEncoder = new AbcEncoder(abcFile, abcContext);
			abcEncoders[i] = abcEncoder;
		}
		
		for (AbcEncoder encoder : abcEncoders) {
			encoder.resetAbcData();
		}
		super.export(m);
	}
}
